//
//  AdressTableViewController.m
//  LoveFreshBeen
//
//  Created by 胡不归是大虾 on 2016/10/20.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "AdressTableViewController.h"
#import "MyAddressViewModel.h"
#import "MyAdressCell.h"
#import "MyAddressViewModel.h"
#import "adressBottomView.h"
#import "EditAddressViewController.h"
@interface AdressTableViewController ()<UITableViewDelegate,UITableViewDataSource,adressBottomViewDelegate>
@property(nonatomic,strong)NSMutableArray* modelArray;
@property(nonatomic,weak) UITableView* tableView;
@property(nonatomic,weak) adressBottomView* bottomView;

@end

@implementation AdressTableViewController
-(instancetype)init{
    if (self = [ super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addBackButton];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    
    [[MyAddressViewModel sharedAdressViewModel] loadData:^(NSMutableArray * array) {
        self.modelArray = array;
    }];
    self.navigationItem.title = @"我的收货地址";
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.tableView.rowHeight = 70;
    self.bottomView.frame = CGRectMake(0, SCREEN_HEIGHT - 64, SCREEN_WIDTH, 64);
}

-(void)setModelArray:(NSMutableArray *)modelArray{
    if ([MyAddressViewModel sharedAdressViewModel].modelArray != nil) {
        _modelArray = [MyAddressViewModel sharedAdressViewModel].modelArray;
    }
    else{
        _modelArray = modelArray;
        [MyAddressViewModel sharedAdressViewModel].modelArray = modelArray;
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyAdressCell* cell = [[[NSBundle mainBundle] loadNibNamed:@"MyAdressCell" owner:nil options:nil] lastObject];
    cell.addressEditBlock = ^(MyAdressModel* model){
        EditAddressViewController* editVC = [[EditAddressViewController alloc] init];
        editVC.model = model;
        editVC.saveBlock = ^(MyAdressModel* model){
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:editVC animated:YES];
    };
    cell.model = self.modelArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)addressBottomView:(adressBottomView *)addressView didClickAddBtn:(UIButton *)btn{
    EditAddressViewController* editVC = [[EditAddressViewController alloc] init];
    editVC.saveBlock =^(MyAdressModel* model){
        [self.modelArray insertObject:model atIndex:0];
        [MyAddressViewModel sharedAdressViewModel].modelArray = self.modelArray;
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:editVC animated:YES];
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        _tableView = tableView;
        [self.view addSubview:tableView];
    }
    return _tableView;
}
-(adressBottomView *)bottomView{
    if (!_bottomView) {
        adressBottomView* bottomView = [[[NSBundle mainBundle] loadNibNamed:@"adressBottomView" owner:nil options:nil] lastObject];
        _bottomView  = bottomView;
        bottomView.delegate = self;
        [self.view addSubview:bottomView];
    }
    return _bottomView;
}
-(void)viewWillAppear:(BOOL)animated{
    [self.tableView reloadData];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == 0) {
        return;
    }
    MyAdressModel *model = [MyAddressViewModel sharedAdressViewModel].modelArray[indexPath.row];
    
    NSNotification *note = [NSNotification notificationWithName:@"address" object:model];
    
    [[NSNotificationCenter defaultCenter]postNotification:note];
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:254/255.0 green:211/255.0 blue:65/255.0 alpha:1]];
    
}

- (void)addBackButton {
    
    self.title = @"我的收货地址";
    UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [back setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
}


- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}



@end
